import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'Login', component: () => import('../components/Login') },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../components/Home'),
    redirect: '/welcome',
    children: [
      { path: '/welcome', name: 'Welcome', component: () => import('../components/Welcome') },
      { path: '/users', name: 'Users', component: () => import('../components/users/Users') },
      { path: '/rights', name: 'Rights', component: () => import('../components/rights/Rights') },
      { path: '/roles', name: 'Roles', component: () => import('../components/rights/Roles') },
      { path: '/categories', name: 'Categories', component: () => import('../components/goods/Categories') },
      { path: '/params', name: 'Params', component: () => import('../components/goods/Params.vue') },
      { path: '/goods', name: 'Goods', component: () => import('../components/goods/Goods.vue') },
      { path: '/goods/add', name: 'Add', component: () => import('../components/goods/Add.vue') },
      { path: '/orders', name: 'Orders', component: () => import('../components/orders/Orders.vue') },
      { path: '/reports', name: 'Reports', component: () => import('../components/reports/Reports.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 全局导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  var token = store.getters.getToken
  if (!token) return next('/login')
  next()
})

export default router
